***define paths
run "${Programs}/texsave_custom.do"


***Who are the clients in the groups 
local new = 1
local groups = " depfp " 
*local with the balance variables
#delimit ;
local balance_vars = " age ado bmi 
	cohabiting married 
	tertiary secondary primary
	salaried self_employed student
	pregnancies children delivery_ever delivery_3mless   
	spacing_nomore spacing_less3yrs spacing_more3yrs 
	current_larc current_sarc current_nonmc mim" ;
#delimit cr
*load dataset
use "${PilotPublicData}/hgopy_pilot_public.dta", clear
if (`new' == 1){
	replace domestic = 1 if (self_employed != 1 & salaried != 1 & student != 1)
}
*get list of binary vars
su `balance_vars'
local binary = "" 
local continuous = ""
qui foreach v of varlist `balance_vars' {
	su `v' , 
	if (`r(min)'==0 & `r(max)'==1) {
		local binary = "`binary' `v'"
	}
	else if !(`r(min)'==0 & `r(max)'==1) {
		local continuous = "`continuous' `v'"
	}
}
*load local with variable labels
qui foreach v of local balance_vars {
	local varlab_`v' : variable label `v'
}
*create the table
iebaltab `balance_vars', grpvar(depfp) control(1) order(1 0) browse rowvarlabels tblnonote format(%9.2f) onerow  std pt starsnoadd total
tempfile x
rename * y*
save `x'
use "${PilotPublicData}/hgopy_pilot_public.dta", clear
iebaltab `balance_vars', grpvar(mind) control(1) order(1 0) browse rowvarlabels tblnonote format(%9.2f) onerow  std pt starsnoadd 
tempfile y
save `y'
*combine the two
use `x'
merge using `y' , 
drop _merge
*clean up 
drop v1
egen x = fill( 0 1 0 1)
replace x=. if _n<=3
foreach v of varlist yv2 yv3 yv4 v2 v3 {
	gen x`v' = `v'[_n+1] if x==1
	order x`v', after(`v')
}
drop if yv1=="" & !missing(x)
drop x
*clean up parentheses
qui foreach v of varlist x* {
	replace `v'=subinstr(`v',"[","(",.)
	replace `v'=subinstr(`v',"]",")",.)	
}
*remove parentheses for dummies
qui foreach v of local binary {
	foreach w of varlist yv2 yv3 yv4 v2 v3 {
		replace x`w'="" if yv1=="`varlab_`v''"
	}
}	 
qui foreach v of local continuous {
	foreach w of varlist yv2 yv3 yv4 v2 v3 {
		replace `w'=`w'+x`w' if yv1=="`varlab_`v''"
	}
}
drop x*
order yv4, after(yv1)
drop if _n<4  
*last little text edit 
replace yv1 = subinstr(yv1,"<=","$\leq$",.) 

*keep only Full Sample + Departments
keep yv1-yv3

* save
  #delimit ;
  texsave_custom using "${OutTexTab}/Tab01-descriptives_nodhs_replication.tex" , replace nonames  hlines(22) frag nofix
    align(" l*{4}{c} ")
    headerlines(" & (1) & (2) & (3) "
                " & \multicolumn{3}{c}{\textbf{Pilot Data}} " 
                " \cmidrule(l){2-4} \cmidrule(lr){5-5} & \textbf{Full Sample} & \textbf{FP} & \textbf{Mat./Gyn}. "
                " & Mean/(SD) & Mean/(SD) & Mean/(SD) ") ;
  #delimit cr

